package com.bkck.sharkblockade.levels;

import com.bkck.sharkblockade.levels.level.Level;
import com.bkck.sharkblockade.menu.R;
import com.bkck.sharkblockade.menu.Menu;
import android.app.Activity;
//import android.content.Intent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
//import android.view.View;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

/**
 * The level selection page.
 * 
 * @author Team BKCK.
 * 
 */
public class LevelSelection extends Activity {

  public static final String TAG = LevelSelection.class.getSimpleName();

  private static SharedPreferences preferences;
  int levelCompleted = 0;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // requesting to turn the title OFF
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    // making it full screen
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.levels);
    Log.d(TAG, "Selection onCreated");

    preferences = getSharedPreferences("USER_SETTINGS", Context.MODE_WORLD_READABLE);

    // Uncomment these lines to unlock all levels.
    SharedPreferences.Editor ed = preferences.edit();
    ed.putInt("lastLevel", 4);
    ed.commit();

    int lastLevel = preferences.getInt("lastLevel", 0);
    levelCompleted = lastLevel;

    Button levelTwo = (Button) findViewById(R.id.levelTwoButton);
    Button levelThree = (Button) findViewById(R.id.levelThreeButton);
    Button levelFour = (Button) findViewById(R.id.levelFourButton);
    Button levelFive = (Button) findViewById(R.id.levelFiveButton);
    Log.d(TAG, "Level Completed: " + levelCompleted);
    if (levelCompleted == 0) {
      levelTwo.setBackgroundResource(R.drawable.locked);
      levelThree.setBackgroundResource(R.drawable.locked);
      levelFour.setBackgroundResource(R.drawable.locked);
      levelFive.setBackgroundResource(R.drawable.locked);
    }
    else if (levelCompleted == 1) {
      levelThree.setBackgroundResource(R.drawable.locked);
      levelFour.setBackgroundResource(R.drawable.locked);
      levelFive.setBackgroundResource(R.drawable.locked);
    }
    else if (levelCompleted == 2) {
      levelFour.setBackgroundResource(R.drawable.locked);
      levelFive.setBackgroundResource(R.drawable.locked);
    }
    else if (levelCompleted == 3) {
      levelFive.setBackgroundResource(R.drawable.locked);
    }
    
  }

  @Override
  protected void onResume() {
    super.onResume();
    Log.d(TAG, "Selection onResume");
  }

  @Override
  protected void onPause() {
    super.onDestroy();
    Log.d(TAG, "Selection onPause");
  }

  @Override
  protected void onDestroy() {
    Log.d(TAG, "Selection onDestroy");
    super.onDestroy();
  }

  @Override
  protected void onStop() {
    Log.d(TAG, "Selection onStop");
    finish();
    super.onDestroy();
  }

  /**
   * Switch to the activity for menu.
   * 
   * @param view The view.
   */
  public void toMenu(View view) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Menu.class.getName());
    startActivity(intent);
  }

  /**
   * Switch to the activity for level one
   * 
   * @param view The view.
   */
  public void toLevelOne(View view) {
    Level.level = 1;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName(this, Level.class.getName());
    startActivity(intent);
  }

  /**
   * Switch to the activity for level two.
   * 
   * @param view The view.
   */
  public void toLevelTwo(View view) {
    if (levelCompleted >= 1) {
      Level.level = 2;
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setClassName(this, Level.class.getName());
      startActivity(intent);
    }
  }

  /**
   * Switch to the activity for level three.
   * 
   * @param view The view.
   */
  public void toLevelThree(View view) {
    if (levelCompleted >= 2) {
      Level.level = 3;
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setClassName(this, Level.class.getName());
      startActivity(intent);
    }
  }

  /**
   * Switch to the activity for level four.
   * 
   * @param view The view.
   */
  public void toLevelFour(View view) {
    if (levelCompleted >= 3) {
      Level.level = 4;
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setClassName(this, Level.class.getName());
      startActivity(intent);
    }
  }

  /**
   * Switch to the activity for level five.
   * 
   * @param view The view.
   */
  public void toLevelFive(View view) {
    if (levelCompleted >= 4) {
      Level.level = 5;
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.setClassName(this, Level.class.getName());
      startActivity(intent);
    }
  }

  /**
   * Ignore screen orientation change.
   */
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }

  /**
   * Returns the preferences file for the application.
   * 
   * @return The preferences for the application.
   */
  public static SharedPreferences getPreferences() {
    return preferences;
  }
}